import http from "@/utils/http"
import cache from "@/assets/js/cache"

export default {
    prefixUrl: "/systemAreaType",
    role: {
        query: "GET:/systemAreaType",
        create: "POST:/systemAreaType",
        select: "GET:/systemAreaType/:id",
        update: "PUT:/systemAreaType/:id",
        delete: "DELETE:/systemAreaType/:id",
        children: "GET:/systemAreaType/:id/children",
    },
    query(params) {
        return http.get(this.prefixUrl, params)
    },
    create(params) {
        return http.post(this.prefixUrl, params)
    },
    select(id) {
        return http.get(this.prefixUrl + "/" + id)
    },
    update(id, params) {
        return http.put(this.prefixUrl + "/" + id, params)
    },
    delete(id) {
        return http.delete(this.prefixUrl + "/" + id)
    },
    cacheClean(id) {
        let url = "/cache" + this.prefixUrl + "/" + id
        cache.clear(url)
    },
    children(id) {
        return http.get(this.prefixUrl + "/" + id + "/children")
    },
    async cache(id) {
        console.log(id)
        let url = "/cache" + this.prefixUrl + "/" + id

        let data = cache.get(url)
        if (!data || !Object.prototype.hasOwnProperty.call(data, "code")) {
            await http
                .get(url)
                .then((res) => {
                    data = res
                    cache.set(url, data)
                })
                .catch((e) => {
                    console.log(e)
                })
        }

        return data
    },
}